package com.isatechindonesia.pilihsaya;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.IntentAction;
import com.mobile.pilihsaya.DBClass;
import com.mobile.pilihsaya.ServiceHelper;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {
	
	public String email,pwd;
	DBClass db = new DBClass(this);
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		
		setContentView(R.layout.activity_login);

		ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar_post);
		actionBar.setTitle("Login");
		
		final EditText txtEmail = (EditText) findViewById(R.id.inputUsr);
		final EditText txtPass = (EditText) findViewById(R.id.inputPass);
		
		TextView btnReg = (TextView) findViewById(R.id.btnRegister);
		TextView btnLupaPass = (TextView) findViewById(R.id.btnLupaPass);
		Button btnLogin = (Button) findViewById(R.id.btnLogin);

		btnReg.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplicationContext(),
						DaftarActivity.class);
				startActivity(i);
			}
		});

		btnLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				email = txtEmail.getText().toString();
				pwd =  txtPass.getText().toString();
				new LoginTask().execute();
			}
		});
		
		btnLupaPass.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplicationContext(), LupaPasswordActivity.class);
				startActivity(i);
			}
		});

	}
	
	public class LoginTask extends AsyncTask<String, Void, Boolean> {

		
		ProgressDialog dialog = new ProgressDialog(LoginActivity.this);

		
		protected void onPreExecute() {
			dialog.setMessage(" Login ");
			dialog.show();
		}

		protected void onPostExecute(Boolean result) {
			dialog.dismiss();
			if (result == true) {
				Toast.makeText(LoginActivity.this, "Login Success",
						Toast.LENGTH_LONG).show();
				Intent i = new Intent(getApplicationContext(), MenuUtamaActivity.class);
				startActivity(i);
				finish();

			} else {
				
				Toast.makeText(LoginActivity.this, "Login Failed",
						Toast.LENGTH_LONG).show();
							}

		}

		@Override
		protected Boolean doInBackground(String... params) {
			// TODO Auto-generated method stub
			ServiceHelper srv = new ServiceHelper();

			if (srv.login(LoginActivity.this, email, pwd)) {
				return true;
			} else {
				return false;
			}
		}

	}

	public static Intent createIntent(Context context) {
		Intent i = new Intent(context, LoginActivity.class);
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		return i;
	}

}
